/**
 * Author zdc 公共函数
 */
function getUrlParameter(property, url) {
	var LocString = url || String(window.location.href);
	var rs = new RegExp("(^|)" + property + "=([^\&]*)(\&|$)", "gi")
			.exec(LocString), tmp;

	if (tmp = rs) {
		return tmp[2].replace(/\s/g, encodeURI(" "));
	}
	return "";
};

function removeByValue(arr, val) {
	for (var i = 0; i < arr.length; i++) {
		if (arr[i] == val) {
			arr.splice(i, 1);
			break;
		}
	}
};

function uuid(){
	var s=[];
	var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";
    var uuid = s.join("");
    return uuid;
};
function bytesToSize(bytes) {
	var sizes = [ 'Bytes', 'KB', 'MB' ];
	if (bytes == 0)
		return 'n/a';
	var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
	return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
};

// 通过文件名，返回文件的后缀名
function fileType(name) {
	var nameArr = name.split(".");
	return nameArr[nameArr.length - 1].toLowerCase();
};

function DataFormate(date) {
	var dateStr = "<空>";
	if (date == undefined || date == "<空>" || $.trim(date) == "") {
		return dateStr;
	} else {
		try {
			var d = new Date(date);
			mouth = '' + (d.getMonth() + 1), day = '' + d.getDate(), year = d
					.getFullYear();
			dateStr = [ year, mouth, day ].join("-");
		} catch (e) {
			console.log(e)
		} finally {
			return dateStr;
		}
	}
}
//计算天数差的函数，通用  
function  DateDiff(sDate1,  sDate2){    //sDate1和sDate2是2006-12-18格式  
    var  aDate,  oDate1,  oDate2,  iDays  
    aDate  =  sDate1.split("-")  
    oDate1  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])    //转换为12-18-2006格式  
    aDate  =  sDate2.split("-")  
    oDate2  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])  
    iDays  =  parseInt((oDate1  -  oDate2)  /  1000  /  60  /  60  /24)    //把相差的毫秒数转换为天数  
    return  iDays  
}
function getUpperWord(ascllno){
	var charno = 65;
	return String.fromCharCode(charno+ascllno);
}
// 将数组中的城市与区分成两个数组
function filterCSAndFQ(arr) {
	debugger
	var flag = {
		fq : false,
		cs : false
	};
	var fqmc = [];
	var csmc = [];
	var layerfilter = null;
	var filter = {};
	csmc = dojo.filter(arr, function(item) {
		if ($.inArray("市", item) > -1) {
			return item;
		}
	});
	fqmc = dojo.filter(arr, function(item) {
		if ($.inArray("区", item) > -1) {
			return item;
		}
	});
	if (fqmc.length > 0) {
		flag.fq = true;
		var filterQ = "FQMC in (";
		for (var i = 0; i < fqmc.length; i++) {
			filterQ += "'" + fqmc[i] + "'";
			if (i < fqmc.length - 1) {
				filterQ += ",";
			}
		}
		filterQ += ")";
		layerfilter = filterQ;
	}
	if (csmc.length > 0) {
		flag.cs = true;
		var filterS = null;
		if (fqmc.length > 0) {
			filterS = " OR CSMC in (";
		} else {
			filterS = " CSMC in (";
		}
		for (var i = 0; i < csmc.length; i++) {
			filterS += "'" + csmc[i] + "'";
			if (i < csmc.length - 1) {
				filterS += ",";
			}
		}
		filterS += ")";
		if (fqmc.length > 0) {
			layerfilter = layerfilter + filterS;
		} else {
			layerfilter = filterS;
		}
	}
	if (flag.cs || flag.fq == true) {
		filter.flag = true;
		filter.layerfilter = layerfilter
	} else {
		filter.flag = false;
	}
	return filter;

}

function skipEmptyElementForArray(arr) {
	var a = [];
	$.each(arr, function(i, v) {
		var data = $.trim(v);
		if ('' != data) {
			if ("null" != data) {
				a.push(data);
			}
		}
	});
	return a;
};

function showLoading() {
	// $('body').showLoading();
	var message = Global.language == "zh-cn" ? "正在加载..." : "Loading...";
	var bc = '<div class="modal-backdrop  in"></div>';
	var load = '<div class="blockui modal" style="display: block;"><div class="modal-dialog"><img class="ball" src="images/loading.gif" /><div class="msg">'
			+ message + '</div></div></div>';
	$('body').append(bc);
	$('body').append(load);
}
function hideLoading() {
	// $('body').hideLoading();
	$('body > .modal-backdrop').remove();
	$('body > .blockui').remove();
}
function showTxtLoading() {
	$('body').showLoading();
}
function hideTxtLoading() {
	$('body').hideLoading();
}
/**
 * 显示自定义的提示消息
 * 
 * @param msg
 *            提示消息
 * @param bError
 *            是否为错误提示
 */
function showMessage(msg, time, dom, bError) {

	require([ "widgets/tooltip/Tooltip" ], function(Tooltip) {
		var speed = 8;// 8字符/秒
		time = Math.ceil(msg.length / speed) * 1000;
		bError = (bError != undefined) ? bError : true;
		new Tooltip({
			"contentStr" : msg,
			time : time,
			relativeParent : dojo.body(),
			bError : bError
		});
	});

}
function confirmInfo(info, callback, title) {
	require([ "widgets/formWindow/confirmInfoWidget/ConfirmWidget" ], function(
			ConfirmWidget) {
		var win = new ConfirmWidget({
			text : info,
			title : title || "",
			callback : callback
		});
		win.startup();
		win.placeAt(dojo.body());
	});
}

/**
 * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换 即谷歌、高德 转 百度
 * 
 * @param lng
 * @param lat
 * @returns {*[]}
 */
function gaodeToBaidu(lng, lat) {
	var x_PI = 3.14159265358979324 * 3000.0 / 180.0;
	var z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_PI);
	var theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_PI);
	var bd_lng = z * Math.cos(theta) + 0.0065;
	var bd_lat = z * Math.sin(theta) + 0.006;
	return [ bd_lng, bd_lat ];
}
function baiduToGaode(bd_lon, bd_lat) {
	var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
	var x = bd_lon - 0.0065;
	var y = bd_lat - 0.006;
	var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
	var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
	var gg_lng = z * Math.cos(theta);
	var gg_lat = z * Math.sin(theta);
	return [ gg_lng, gg_lat ]
}

// 百分比四舍五入后归一
function evenRound(arr) {
	var decimal = -~arr.map(function(a) {
		return a % 1;
	}).reduce(function(a, b) {
		return a + b;
	}); // Ceil of total sum of decimals
	for (var i = 0; i < decimal; ++i) {
		arr[i] = ++arr[i]; // compensate error by adding 1 the the first n
		// items
	}
	return arr.map(function(a) {
		return ~~a;
	}); // floor all other numbers
}
// 因iphone单选框风格异常做的调整
function resetPhoneRadioBtn(node) {
	if (Global.device == 'phone') {
		var domsY, domsN;
		if (node) {
			domsY = dojo.query(".en-yes", node);
			domsN = dojo.query(".en-no", node);
		} else {
			domsY = dojo.query(".en-yes");
			domsN = dojo.query(".en-no");
		}
		if ((domsY && domsN) && domsY.length == domsN.length) {
			for (var i = 0; i < domsY.length; i++) {
				var tmpYtxt = $(domsY[i]).text();
				var tmpNtxt = $(domsN[i]).text();
				domsY[i].innerHTML = dojo.query("input", domsY[i])[0].outerHTML
						+ tmpNtxt;
				domsN[i].innerHTML = dojo.query("input", domsN[i])[0].outerHTML
						+ tmpYtxt;
			}
		}
	}
}
function formatDate(date,splitstr) {
	var year = date.getYear() + 1900;
	var month = date.getMonth() + 1;
	var day = date.getDate();
	var hour = date.getHours();
	var minute = date.getMinutes();
	var second = date.getSeconds();
	if(splitstr!=null){
		return year + splitstr + month + splitstr + day ;
	}else{
		return year + "年" + month + "月" + day + "日 ";// +hour+":"+(minute<10?"0"+minute:minute)+":"+(second<10?"0"+second:second);
	}
}
function numberFormate(number) {
	if (parseFloat(number).toFixed(2) != 'NaN') {
		return parseFloat(number).toFixed(2);
	} else {
		return 0;
	}
}
function numberWanFormate(number) {
	if (parseFloat(number).toFixed(4) != 'NaN') {
		return parseFloat(number).toFixed(4);
	} else {
		return 0;
	}
}
function getDateStr() {
	var date = new Date();
	var year = date.getYear() + 1900;
	var month = date.getMonth() + 1;
	var day = date.getDate();
	var hour = date.getHours();
	var minute = date.getMinutes();
	var second = date.getSeconds();
	return year + "" + (month < 10 ? "0" + month : month) + ""
			+ (day < 10 ? "0" + day : day) + ""
			+ (hour < 10 ? "0" + hour : hour) + ""
			+ (minute < 10 ? "0" + minute : minute) + ""
			+ (second < 10 ? "0" + second : second);
}
function reduceformatDate(days, splitstr) {
	var val = days * 24 * 60 * 60 * 1000;
	var targetdate = new Date(new Date().getTime() - val);
	var year = targetdate.getYear() + 1900;
	var month = targetdate.getMonth() + 1;
	var day = targetdate.getDate();
	// return targetdate.getTime();
	return year + splitstr + (month > 9 ? month : "0" + month) + splitstr
			+ (day > 9 ? day : "0" + day);
}
function stringToData(datestr){
	datestr = datestr.replace(/-/g,"/");
	var targetdate = new Date(datestr );
	var year = targetdate.getYear() + 1900;
	var month = targetdate.getMonth() + 1;
	var day = targetdate.getDate();
	return [year,month,day];
}
var dateFormatToString = function(value, key, data) {
	if (value == null || value.trim() == '') {
		return "";
	}
	return formatDate(new Date(value));
};
function listSortBy(arr, field, order, type) {
	debugger
	var refer = [], result = [], order = order == 'asc' ? 'asc' : 'desc', index;
	for (var i = 0; i < arr.length; i++) {
		var obj = {
			val : arr[i][field],
			idx : i
		};
		refer.push(obj);

	}
	if (type == "Number") {
		// refer.sort();
		refer.sort(function(a, b) {
			return a.val - b.val
		});
	} else if (type == "ChineseText") {
		refer.sort(function(a, b) {
			return a.val.localeCompare(b.val); // output:之,家,本,脚
		});
	}
	if (order == 'desc') {
		refer.reverse();
	}
	for (i = 0; i < refer.length; i++) {
		index = refer[i].idx;
		result[i] = arr[index];
	}
	return result;
}
